<?php
/**
* session memcache处理类
*/
class SessionMemcache extends AbsSessionHandle {
	private $mem;
	
	public function __construct($cfg) {
		//$session_save_path = 'tcp://'.$this->cfg['memcache_server'].':'.$this->cfg['memcache_port'].'?persistent=1&weight=2&timeout=2&retry_interval=10';
		//ini_set('session.save_handler', 'memcache');
		//ini_set('session.save_path', $session_save_path);
		parent::__construct($cfg);
		
		if($this->cfg['memcache_pconnect']) {
			$conn_fn = 'memcache_pconnect';
		} else {
			$conn_fn = 'memcache_connect';
		}
		$this->mem = $conn_fn($this->cfg['memcache_server'], $this->cfg['memcache_port'], $this->cfg['memcache_timeout']);
	}
	
	public function open($savepath, $name) {
		return TRUE;
	}
	
	public function close() {
		return memcache_close($this->mem);
	}
	
	public function read($id) {
		return (string) memcache_get($this->mem, $id);
	}
	
	public function write($id, $sess_data) {
		//memcache_set遇到没有的key会自动添加
		memcache_set($this->mem, $id, $sess_data, NULL, $this->lifetime);
	}
	
	public function destroy($id) {
		return memcache_delete($this->mem, $id);
	}
	
	public function gc($maxlifetime) {
		return TRUE;
	}
}
?>